#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include "Date.h"

Date readDate() {
	std::cout << "==================================DATES==================================" << std::endl;
	std::cout << "Enter 1 - to enter current date, 2 - to enter arbitrary date" << std::endl;
	std::cout << "=========================================================================" << std::endl;
	

	int workWithDatesMode;
	std::cin >> workWithDatesMode;

	switch( workWithDatesMode )
	{

	case 1:
		{
			Date date;
			return date;
		}
		break;
	case 2:
		{
			std::cout << "Enter date in format YYYY MM DD hh mm ss" << std::endl;
			unsigned int year, month, day, hour, minute, second;
			std::cin >> year >> month >> day >> hour >> minute >> second;
			Date otherDate(year, (Date::Months) month, day, hour, minute, second);
			return otherDate;
		}
		break;
	default:
		throw std::logic_error("MESSAGE: Incorrect value");
		break;

	}
}

DateInterval readDateInterval() { 
	int year, month, day, hour, minute, second;
	std::cout << "MESSAGE: Enter interval in YYYY MM DD hh mm ss"<< std::endl;
	std::cin >> year >> month >> day >> hour >> minute >> second;
	DateInterval interval( year, month, day, hour, minute, second );

	return interval;
}

void printInterval(const DateInterval& interval) {
	std::cout << interval.getYearDifference() << " " << interval.getMonthDifference() << " " <<
		interval.getDayDifference() << " " << interval.getHourDifference() << ":" << interval.getMinuteDifference()
		<< ":" << interval.getSecondDifference()<< std::endl;
}
void intervalCalculator ()
{
	try {
		Date firstDate = readDate();
		Date secondDate = readDate();
		DateInterval interval = firstDate.getInterval(secondDate);
		std::cout << "MESSAGE: Difference between current and your date is: " << std::endl;
		printInterval(interval);
		std::cout << std::endl;
	} catch( std::exception& ) {
		std::cout << "MESSAGE: You enter incorrect date"<< std::endl;
	}
};

void addIntervalToDate ()
{
	try {
		Date firstDate = readDate();
		std::cout << "You enter date: " << firstDate.formatDate("YYYY MMM DD hh:mm:ss") << std::endl;
		DateInterval interval = readDateInterval ();
		Date newDate = firstDate.addInterval(interval);
		std::cout << "MESSAGE: Modified date: " << std::endl << newDate.formatDate("YYYY MMM DD hh:mm:ss")<< std::endl;

	} catch( std::exception& ) {
		std::cout << "MESSAGE: Something was wrong. Lean to the back of your chair and wait for a moment"<< std::endl;
	}


};

void workWithDateInterval() {

	for( ;; )
	{
		std::cout << "======================MENU OF WORK WITH DATE INTERVAL====================" << std::endl;
		std::cout << "Enter mode of operation: 1 - to calculate the interval between dates," << std::endl;
		std::cout << "2 - to add an interval to date 0 - to exit" << std::endl;
		std::cout << "=========================================================================" << std::endl;
		
		int calculatorMode;
		std::cin >> calculatorMode;

		switch (calculatorMode)
		{
		case 0:
			return;
			break;
		case 1:
			intervalCalculator ();
			break;
		case 2:
			addIntervalToDate ();
			break;
		}
	}	
}

void dateCalculator ( const Date& date )
{
	Date workDate = date;

	for( ;; )
	{
		std::cout << "You enter date: " << std::endl << workDate.formatDate("YYYY MMM DD hh:mm:ss")<< std::endl;
		std::cout << "==============================MENU OF CALCULATOR=========================" << std::endl;
		std::cout << "Enter to add 1 - seconds,2 - minutes, 3 - hours, 4 - days, 5 - months," << std::endl;
		std::cout << "6 - years, 0 - to exit"  << std::endl;
		std::cout << "=========================================================================" << std::endl;

		int calculatorMode;
		std::cin >> calculatorMode;

		switch (calculatorMode)
		{
		case 0:	
			return ;
			break;
		
		case 1:
			std::cout << "MESSAGE: Enter count of seconds" << std::endl;
			int second;
			std::cin >> second;
			try
			{
				workDate = workDate.addSecond (second);
			} catch( std::exception &) {
				std::cout << "MESSAGE: You enter incorrect value" << std::endl;
			}
			break;
		
		case 2:
			std::cout << "MESSAGE: Enter count of minutes" << std::endl;
			int minute;
			std::cin >> minute;
			try
			{
				workDate = workDate.addMinute (minute);
			} catch( std::exception &) {
				std::cout << "MESSAGE: You enter incorrect value" << std::endl;
			}
			break;
		
		case 3:
			std::cout << "MESSAGE: Enter count of hours" << std::endl;
			int hour;
			std::cin >> hour;
			try
			{
				workDate = workDate.addHour(hour);
			} catch( std::exception &) {
				std::cout << "MESSAGE: You enter incorrect value" << std::endl;
			}
			break;
		
		case 4:
			std::cout << "MESSAGE: Enter count of days" << std::endl;
			int day;
			std::cin >> day;
			try
			{
				workDate = workDate.addDay (day);
			} catch( std::exception &) {
				std::cout << "MESSAGE: You enter incorrect value" << std::endl;
			}
			break;
		
		case 5:
			std::cout << "MESSAGE: Enter count of months" << std::endl;
			int months;
			std::cin >> months;
			try
			{
				workDate = workDate.addMonth(months);
			} catch( std::exception &) {
				std::cout << "MESSAGE: You enter incorrect value" << std::endl;
			}
			break;
		
		case 6:
			std::cout << "MESSAGE: Enter count of years" << std::endl;
			int year;
			std::cin >> year;
			try
			{
				workDate = workDate.addYear (year);
			} catch( std::exception &) {
				std::cout << "MESSAGE: You enter incorrect value" << std::endl;
			}
			break;

		}
	}
}

void workWithDates ()
{
	std::cout << "===========================MENU OF WORK WITH DATE========================" << std::endl;
	std::cout << "Enter 1 - to enter current date, 2 - to enter arbitrary date, 0 - to exit" << std::endl;
	std::cout << "=========================================================================" << std::endl;
	
	int workWithDatesMode;
	std::cin >> workWithDatesMode;

	switch( workWithDatesMode )
	{
	case 0:
		return;
		break;
	case 1:
		{
			Date date;
			dateCalculator(date);
		}
		break;
	case 2:
		{
			unsigned int year, month, day, hour, minute, second;
			std::cin >> year >> month >> day >> hour >> minute >> second;
			Date otherDate(year, (Date::Months) month, day, hour, minute, second);
			dateCalculator(otherDate);
		}
		break;

	}
}
